package com.enhinck.dbutilweb.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 描述
 *
 * @author huenbin
 * @date 1/15/21 10:38 AM
 */
@Slf4j
public class FileProcessFactory {

    List<IFileProcess> iFileProcesses = new ArrayList<>();

    public FileProcessFactory() {
        PackageScanner scan = new ClasspathPackageScanner("com.enhinck.dbutilweb.util.file");
        try {
            List<String> list = scan.getFullyQualifiedClassNameList();
            list.forEach(item -> {
                try {
                    Class<?> clazz = Class.forName(item);
                    if (clazz.getSuperclass().isAssignableFrom(IFileProcess.class)) {
                        Object instance = clazz.newInstance();
                        iFileProcesses.add((IFileProcess) instance);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void processFile(File file) {
        String fileName = file.getName();
        if (fileName.contains(".")) {
            String ext = StringUtils.substringAfterLast(fileName, ".");
            for (IFileProcess fileProcess : iFileProcesses) {
                if (fileProcess.support(ext)) {
                    fileProcess.process(file);
                    break;
                }
            }
        }
    }


    public void copyFileTo(String path) {
        final String newPath = path;
        CountDownLatch countDownLatch = new CountDownLatch(iFileProcesses.size());
        for (IFileProcess fileProcess : iFileProcesses) {
            new Thread(() -> {
                final String catalogPath = newPath + "/" + fileProcess.newFolderName();
                fileProcess.getGroups().forEach((date, list) -> {
                    final String datePath = catalogPath + "/" + date;
                    list.forEach(file -> {
                        File folder = new File(datePath + "/");
                        if (!folder.exists()) {
                            folder.mkdirs();
                        }
                        String dest = datePath + "/" + file.getName();
                        File destFile = new File(dest);
                        try {
                            destFile.createNewFile();
                            FileUtils.copyFile(file, new File(dest));
                        } catch (Exception e) {
                            log.error("文件拷贝异常{}", e);
                        }
                    });
                });
                countDownLatch.countDown();
            }).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.info("文件整理完毕");
    }

}
